package com.zjw.service;

import com.zjw.constants.SystemConstants;
import com.zjw.utils.SecurityUtil;
import org.springframework.stereotype.Service;

@Service("Ps")
public class PermissionService {


    /**
     * 当前用户是否有指定权限
     *
     * @param perm 权限
     * @return {@link Boolean}
     */
    public Boolean hasPermission(String perm) {
        if (SecurityUtil.getCurUserId() == 1 || SecurityUtil.getCurUserId() == 7) {
//            超管，无需权限信息直接放行
            return true;
        }
//        System.out.println("是否有：" + perm + ":" + SecurityUtil.getCurUserAuthorities().contains(perm));
//        获取当前用户权限信息，查询是否拥有
        if (SecurityUtil.getCurUserAuthorities() == null) {
            return false;
        }
        return SecurityUtil.getCurUserAuthorities().contains(perm);
    }


    /**
     * 当前用户是否有指定角色
     *
     * @param role 角色
     * @return {@link Boolean}
     */
    public Boolean hasRole(String role) {
        if (SecurityUtil.getCurUserId() == 1 || SecurityUtil.getCurUserId() == 7) {
//            超管，无需权限信息直接放行
            return true;
        }
//        获取当前用户权限信息，查询是否拥有
//        System.out.println("是否有" + role + ":" + SecurityUtil.getCurUserAuthorities().contains(SystemConstants.AUTHORITIES_ROLE_PREFIX + role));
        return SecurityUtil.getCurUserAuthorities().contains(SystemConstants.AUTHORITIES_ROLE_PREFIX + role);
    }


}
